package com.ebt.m.customer.view;

import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.cache.CustomerGetStateCache;
import com.ebt.m.customer.constant.CustomerConstant;
import com.ebt.m.customer.item.Contact;
import com.ebt.m.customer.listener.IView;
import com.ebt.m.customer.model.CustomerGetState;
import com.ebt.m.customer.service.FetchCustomerService;
import com.ebt.m.customer.util.CustomerRecorder;
import com.ebt.m.data.CustomerData;
import com.ebt.m.data.entity.Customer;
import com.ebt.m.homepage.GetCustomerEvent;
import com.tendcloud.tenddata.TCAgent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;


public class CustomerGetView extends LinearLayout implements IView, View.OnClickListener{

    private static final String CANCEL_IGNORE = "取消忽略";
    private static final String IGNORE = "忽略";
    private static final String IMPORT = "导入";
    private static final String HAS_IMPORT = "已导入";

	private Context mContext;

    private AvatarView fragment_customer_get_iv_avatar;
	private TextView fragment_customer_get_tv_name;
	private Button fragment_customer_get_btn_import;
	private Button fragment_customer_get_btn_ignore;
	
	private Contact mContact;
	private CustomerData mCustomerData;
	private CustomerGetStateCache mCustomerGetStateCache;

    private Drawable mDrawableImport;
    private Drawable mDrawableHasImport;
    private ColorStateList mColorImport;
    private ColorStateList mColorHasImport;
	
	public CustomerGetView(Context context) {
		this(context, null);

    }

	public CustomerGetView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	
	public CustomerGetView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		View contentView = inflate(context, R.layout.view_customer_get, this);
		
		fragment_customer_get_iv_avatar = (AvatarView)contentView.findViewById(R.id.fragment_customer_get_iv_avatar);
		fragment_customer_get_tv_name = (TextView)contentView.findViewById(R.id.fragment_customer_get_tv_name);
		fragment_customer_get_btn_import = (Button)contentView.findViewById(R.id.fragment_customer_get_btn_import);
		fragment_customer_get_btn_ignore = (Button)contentView.findViewById(R.id.fragment_customer_get_btn_ignore);
		
		fragment_customer_get_btn_import.setOnClickListener(this);
		fragment_customer_get_btn_ignore.setOnClickListener(this);

        mDrawableHasImport = mContext.getResources().getDrawable(R.drawable.bg_green_rect_2_for_disable);
        mColorHasImport = mContext.getResources().getColorStateList(R.drawable.tc_customer_get_white_to_green);

        mDrawableImport = mContext.getResources().getDrawable(R.drawable.bg_green_rect);
        mColorImport = mContext.getResources().getColorStateList(R.drawable.tc_customer_get_white);

	}

	@Override
	public View getView() {
		return this;
	}

	@Override
	public void update(Object o) {
		mContact = (Contact)o;
		
		fragment_customer_get_tv_name.setText(mContact.name);
		fragment_customer_get_iv_avatar.setTag(o);
        AvatarView.updateAvatarView(fragment_customer_get_iv_avatar,
                                    mCustomerGetStateCache.bitmapCache,
                                    mContact.rawContactId,
                                    null,
                                    mContact.name);
        CustomerGetState itemState = mCustomerGetStateCache.customerGetCache.get(mContact.rawContactId);

        if(itemState != null){
            if(itemState.isIgnored){//已忽略
                fragment_customer_get_btn_ignore.setText(CANCEL_IGNORE);
                fragment_customer_get_btn_ignore.setEnabled(true);
                if(itemState.isImported){//已忽略+已导入
                    //不会走这里
                }else{//已忽略+未导入
                    fragment_customer_get_btn_import.setText(IMPORT);
                    fragment_customer_get_btn_import.setEnabled(false);
                    fragment_customer_get_btn_import.setTextColor(mColorImport);
                    fragment_customer_get_btn_import.setBackgroundDrawable(mDrawableImport);
                }
            }else{//未忽略
                if(itemState.isImported){//已导入+未忽略
                    fragment_customer_get_btn_import.setText(HAS_IMPORT);
                    fragment_customer_get_btn_import.setEnabled(false);
                    fragment_customer_get_btn_import.setTextColor(mColorHasImport);
                    fragment_customer_get_btn_import.setBackgroundDrawable(mDrawableHasImport);
                    fragment_customer_get_btn_ignore.setText(IGNORE);
                    fragment_customer_get_btn_ignore.setEnabled(false);
                }else{//未导入+未忽略
                    fragment_customer_get_btn_import.setText(IMPORT);
                    fragment_customer_get_btn_import.setEnabled(true);
                    fragment_customer_get_btn_import.setTextColor(mColorImport);
                    fragment_customer_get_btn_import.setBackgroundDrawable(mDrawableImport);
                    fragment_customer_get_btn_ignore.setText(IGNORE);
                    fragment_customer_get_btn_ignore.setEnabled(true);
                }
            }
        }else {
            if (!mContact.hasIgnored) {//未忽略,未导入
                fragment_customer_get_btn_import.setText(IMPORT);
                fragment_customer_get_btn_import.setEnabled(true);
                fragment_customer_get_btn_ignore.setText(IGNORE);
                fragment_customer_get_btn_ignore.setEnabled(true);
                fragment_customer_get_btn_import.setTextColor(mColorImport);
                fragment_customer_get_btn_import.setBackgroundDrawable(mDrawableImport);
            }else{//已忽略,未导入
                fragment_customer_get_btn_ignore.setText(CANCEL_IGNORE);
                fragment_customer_get_btn_ignore.setEnabled(true);
                fragment_customer_get_btn_import.setText(IMPORT);
                fragment_customer_get_btn_import.setEnabled(false);
                fragment_customer_get_btn_import.setTextColor(mColorImport);
                fragment_customer_get_btn_import.setBackgroundDrawable(mDrawableImport);
                CustomerGetState newitemState = new CustomerGetState();
                newitemState.isIgnored = true;
                mCustomerGetStateCache.customerGetCache.put(mContact.rawContactId, newitemState);
            }
        }
	}

	@Override
	public void onClick(View v) {
		if(mCustomerData == null){
			mCustomerData = new CustomerData(mContext);
		}
		CustomerGetState itemState = mCustomerGetStateCache.customerGetCache.get(mContact.rawContactId);
		if(itemState == null){
			itemState = new CustomerGetState();
		}
		
		switch(v.getId()){
		case R.id.fragment_customer_get_btn_import:
			String retImportCustomerUuid = mCustomerData.insertCustomerFromContact(mContact, true);

            if(!TextUtils.isEmpty(retImportCustomerUuid)){
                mContact.setCustomerUuid(retImportCustomerUuid);
                importOtherInfo(mContact);

				itemState.isImported = true;
				fragment_customer_get_btn_import.setText(HAS_IMPORT);
                fragment_customer_get_btn_import.setEnabled(false);
                fragment_customer_get_btn_import.setTextColor(mColorHasImport);
                fragment_customer_get_btn_import.setBackgroundDrawable(mDrawableHasImport);
                fragment_customer_get_btn_ignore.setEnabled(false);
				CustomerRecorder.recordCustomer(mContext, retImportCustomerUuid, CustomerRecorder.CUSTOMER_ADDED, "导入新客户");
				TCAgent.onEvent(mContext, "import_customer_from_huoke");
                EventBus.getDefault().post(new Customer());
			}else{
				itemState.isImported = false;
			}
			
			break;
		case R.id.fragment_customer_get_btn_ignore:
			if(!itemState.isIgnored) {//原来状态：没有忽略
                String retIgnoreCustomerUuid = mCustomerData.insertCustomerFromContact(mContact, false);

                if (!TextUtils.isEmpty(retIgnoreCustomerUuid)) {
                    itemState.isIgnored = true;
                    fragment_customer_get_btn_ignore.setText(CANCEL_IGNORE);
                    fragment_customer_get_btn_ignore.setEnabled(true);
                    fragment_customer_get_btn_import.setText(IMPORT);
                    fragment_customer_get_btn_import.setEnabled(false);
                    fragment_customer_get_btn_import.setTextColor(mColorImport);
                    fragment_customer_get_btn_import.setBackgroundDrawable(mDrawableImport);
                } else {
                    itemState.isIgnored = false;
                }
            }else{//原来状态：已经忽略
                itemState.isIgnored = false;
                fragment_customer_get_btn_ignore.setText(IGNORE);
                fragment_customer_get_btn_ignore.setEnabled(true);
                fragment_customer_get_btn_import.setText(IMPORT);
                fragment_customer_get_btn_import.setEnabled(true);
                fragment_customer_get_btn_import.setTextColor(mColorImport);
                fragment_customer_get_btn_import.setBackgroundDrawable(mDrawableImport);
                mCustomerData.removeIgnoredCustomerFromContact(mContact);
            }
			break;
		}
		mCustomerGetStateCache.customerGetCache.put(mContact.rawContactId, itemState);
        EventBus.getDefault().post(new GetCustomerEvent());
    }

    private void importOtherInfo(Contact contact){
        ArrayList<Contact> contactsWithCUuid = new ArrayList<>();
        contactsWithCUuid.add(contact);
        Intent startIntent = new Intent(this.getContext(), FetchCustomerService.class);
        startIntent.putExtra(CustomerConstant.SERVICE_FETCH_MODE, CustomerConstant.SERVICE_FETCH_CUSTOMER_IMPORT_OTHER);
        startIntent.putExtra(CustomerConstant.SERVICE_FETCH_CUSTOMER_IMPORT_OTHER_KEY, contactsWithCUuid);
        this.getContext().startService(startIntent);
    }
	
	@Override
	public void setStateCache(Object o) {
		mCustomerGetStateCache = (CustomerGetStateCache)o;
	}
}